﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KingCat_Beans;
using KingCat_Socket;

namespace KingCat_Client
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
            this.pictureBox2.Image = Image.FromFile(@"..\..\newFaces\" + "86.bmp");
            this.comboBox1.Text = Convert.ToString(86);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



        /// <summary>
        /// 检查用户注册信息的合法性功能
        /// </summary>
        /// <returns></returns>
        private Boolean userValidate()
        {
            Boolean flag = true;
            if ("".Equals(this.txtNickName.Text) || txtNickName.Text.Contains("|") == true)
            {
                this.label11.ForeColor = Color.Red;
                this.label11.Visible = true;
                flag = false;
            }
            try
            {
                Convert.ToInt16(txtAge.Text);
            }
            catch (Exception e1)
            {
                this.label12.ForeColor = Color.Red;
                this.label12.Visible = true;
                flag = false;
            }
            if (txtPassword.Text.Length < 6)
            {
                this.label14.ForeColor = Color.Red;
                this.label14.Visible = true;
                flag = false;
            }
            if (txtPassword.Text.Length <= 6 && !txtPassword.Text.Equals(txtPassword2.Text) || txtPassword.Text.Contains("|") == true)
            {
                this.label13.ForeColor = Color.Red;
                this.label13.Visible = true;
                flag = false;
            }
            if (checkBox1.Checked == false)
            {
                this.label15.ForeColor = Color.Red;
                this.label15.Visible = true;
                flag = false;
            }

            if (textBox4.Text != "" && (textBox4.Text.IndexOf("@") == -1 || textBox4.Text.IndexOf("@") == textBox4.Text.Length - 1) || textBox4.Text.Contains("|") == true)
            {
                this.label16.ForeColor = Color.Red;
                this.label16.Visible = true;
                flag = false;
            }
            return flag;

        }





        /// <summary>
        /// 处理用户注册填写的信息功能
        /// </summary>
        /// <returns>返回包含用户所有注册信息的对象</returns>
        public UserInfo registerInfo()
        {
            UserInfo userInfo = new UserInfo();
            userInfo.userName = txtNickName.Text;
            userInfo.password = txtPassword.Text;
            userInfo.age = txtAge.Text;
            if (rdbMale.Checked == true)
            { userInfo.sex = "男"; }
            if (rdbFemale.Checked == true)
            {
                userInfo.sex = "女";
            }
            userInfo.picture = Convert.ToString(comboBox1.SelectedIndex);
            userInfo.trueName = txtName.Text;
            userInfo.constellation = cboStar.Items[cboStar.SelectedIndex + 1].ToString();
            userInfo.blooed = cboBloodType.Items[cboBloodType.SelectedIndex + 1].ToString();
            userInfo.email = textBox4.Text;
            return userInfo;
        }




        /// <summary>
        /// 处理用户注册功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegister_Click(object sender, EventArgs e)
        {

            this.label11.Visible = false;
            this.label12.Visible = false;
            this.label13.Visible = false;
            this.label14.Visible = false;
            this.label15.Visible = false;
            this.label16.Visible = false;
            //检查用户填写的注册信息是否有效
            Boolean flag = userValidate();
            //向服务器发送用户注册信息
            if (flag == true)
            {
                //获得用户注册的信息
                UserInfo userInfo = registerInfo();
                String info = "用户注册|" + userInfo.userName + "|" + userInfo.password + "|" + userInfo.age + "|" + userInfo.sex + "|" + userInfo.picture
                    + "|" + userInfo.trueName + "|" + userInfo.constellation + "|" + userInfo.blooed + "|" + userInfo.email;
                //将用户信息发送到服务器
                if (ConnectServerSocket.getInstance().getStreamWriter() == null)
                {
                    MessageBox.Show("请先输入服务器ip与端口号连接服务器!", "温馨提示");
                }
                else
                {
                    ConnectServerSocket.getInstance().getStreamWriter().WriteLine(info);
                    ConnectServerSocket.getInstance().getStreamWriter().Flush();
                    String result = ConnectServerSocket.getInstance().getStreamReader().ReadLine();
                    if (result == "true")
                    {
                        MessageBox.Show("恭喜注册成功，现可登入系统！", "温馨提示");
                        this.Visible = false;
                        new Login().ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("对不起，现在服务器停止注册，请联系管理员！", "温馨提示");
                    }
                }

            }
        }




        /// <summary>
        /// 出来用户自定义头像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filename = @"..\..\newFaces\" + (comboBox1.SelectedIndex + 1) + ".bmp";
            pictureBox2.Image = Image.FromFile(filename);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }
    }
}